package net.uzen.office.article.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.uzen.office.article.dao.ArticleDao;
import net.uzen.office.article.model.Article;
import net.uzen.office.attached.dao.AttachedDao;
import net.uzen.office.attached.model.Attached;
import net.uzen.office.common.UzenOfficeConstants;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;

public class ArticleServiceImpl implements ArticleService {

	private static final Log logger = LogFactory
			.getLog(ArticleServiceImpl.class);

	private ArticleDao articleDao;
	private AttachedDao attachedDao;

	public void setArticleDao(ArticleDao articleDao) {
		this.articleDao = articleDao;
	}

	public void setAttachedDao(AttachedDao attachedDao) {
		this.attachedDao = attachedDao;
	}

	@Override
	public int createArticle(Article article) {

		// 게시물 등록
		int articleNo = articleDao.createArticle(article);

		// 싱글 파일 업로드는 아래를 이용
		Attached attached = new Attached();
		BeanUtils.copyProperties(article, attached);

		// URL이 첨부되었을 때
		if (StringUtils.isNotEmpty(article.getAttachedUrl())) {
			logger.debug("[!] ArticleServiceImpl >> Url");
			attached.setAttachedType(UzenOfficeConstants.ATTACHED_TYPE_URL);
			attached.setAttachedValue(article.getAttachedUrl());
			attachedDao.createAttached(attached);
		}

		// 파일이 첨부되었을 때
		if (StringUtils.isNotEmpty(article.getAttachedFilename())) {
			logger.debug("[!] ArticleServiceImpl >> File");
			attached.setAttachedType(UzenOfficeConstants.ATTACHED_TYPE_FILE);
			attached.setAttachedValue(article.getAttachedFile()
					.getOriginalFilename());
			attached.setAttachedValue2(article.getAttachedFilename());
			attachedDao.createAttached(attached);
		}

		if (StringUtils.isNotEmpty(article.getAttachedLocation())) {
			logger.debug("[!] ArticleServiceImpl >> Location");
			// 위치가 첨부되었을 때
			attached
					.setAttachedType(UzenOfficeConstants.ATTACHED_TYPE_LOCATION);
			article.setAttachedLocation(article.getAttachedLocation()
					.substring(1, article.getAttachedLocation().length() - 1));
			String[] latlng = article.getAttachedLocation().split(",");
			attached.setAttachedValue(latlng[0].trim());
			attached.setAttachedValue2(latlng[1].trim());
			logger.debug("[!] latlng[0] >> " + latlng[0]);
			logger.debug("[!] latlng[1] >> " + latlng[1]);
			attachedDao.createAttached(attached);
		}

		// 추후에 멀티 파일 업로드가 되면 할 수 있다.
		if (article.getAttachedList() != null
				&& !article.getAttachedList().isEmpty()) {
			logger.debug("[!] ArticleServiceImpl >> Multiple File");
			for (Attached attached2 : article.getAttachedList()) {
				attached2.setArticleNo(articleNo);
				attached2.setBoardNo(article.getBoardNo());
				// attachedDao.createAttached(attached);
			}
		}
		return articleNo;
	}

	@Override
	public boolean deleteArticle(Article article) {
		return articleDao.deleteArticle(article);
	}

	@Override
	public Map<String, Object> getArticle(Article article) {

		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("article", articleDao.getArticle(article));
		resultMap.put("attachedList", attachedDao.getAttachedList(article));

		return resultMap;
	}

	@Override
	public List<Article> getArticleList(Article article) {
		return articleDao.getArticleList(article);
	}

	@Override
	public boolean modifyArticle(Article article) {
		return articleDao.modifyArticle(article);
	}

	@Override
	public int replyArticle(Article article) {
		// 게시물 순서를 업데이트하기 위한 객체
		Article updateArticlesOrder = new Article();

		// 답변 게시물의 순서 가져온다.
		int articleOrder = articleDao.getArticleOrder(article);
		updateArticlesOrder.setArticleOrder(articleOrder);

		// 부모 게시물 정보를 가져온다.
		Article parentArticle = articleDao.getArticle(article);

		updateArticlesOrder.setBoardNo(parentArticle.getBoardNo());
		updateArticlesOrder.setThread(parentArticle.getThread());

		// 새로 등록될 게시물 순서 이후의 게시물 순서를 1씩 증가시킨다.
		articleDao.updateArticlesOrder(updateArticlesOrder);

		// 답변 게시물을 등록한다.
		article.setArticleOrder(articleOrder);
		article.setThread(parentArticle.getThread());

		return articleDao.createArticle(article);
	}
}
